#pragma once
#include<iostream>
#include<string>
#include "log.hpp"
#include "Dict.hpp"

Dict dict;

extern Log lg;

class Task
{
public:
    Task(){}

    Task(int sockfd, const std::string& clientip, const uint16_t &clientport)
    :sockfd_(sockfd), clientip_(clientip),clientport_(clientport)
    {}
    void run()
    {
        char buffer[4096];
        ssize_t n = read(sockfd_, buffer,sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;//-2是为了获取telnet中读取的数据是有效的
            std::cout << "client say# " << buffer << std::endl;
            std::string echo_str = dict.Translation(buffer);
            int ret = write(sockfd_, echo_str.c_str(),echo_str.size());
            if(ret == -1) // 还是需要对写操作进行差错处理
            {
                lg(Warning, "write error, errno : %d , errstr : %s", errno, strerror(errno));
            }

        }
        else if(n == 0)
        {
            lg(Info,"%s,%d quit, server close sockfd : %d",clientip_.c_str(), clientport_, sockfd_);

        }
        else
        {
            lg(Warning, "read error , socket_:%d, client ip :%s, client port :%d",sockfd_, clientip_.c_str(), clientport_);
        }
        close(sockfd_);
}
    ~Task()
    {

    }
    std::string GetTask()
    {

    }
    
    void operator()()//仿函数
    {
        run();
    }
private:
    int sockfd_;
    std::string clientip_;
    uint16_t clientport_;
};